﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FubiManger.UI;
using DAL;
using Model;
using System.Web.Script.Serialization;
using Smobiler.Device;

namespace FubiManger.Controls
{
    ////ToolboxItem用于控制是否添加自定义控件到工具箱，true添加，false不添加
    //[System.ComponentModel.ToolboxItem(true)]
    partial class ordercontrol : Smobiler.Core.Controls.MobileUserControl
    {
        public ordercontrol() : base()
        {
            //This call is required by the SmobilerUserControl.
            InitializeComponent();
        }
        /// <summary>
        /// 昵称
        /// </summary>
        public string nickname { get; set; }
        /// <summary>
        /// 订单号
        /// </summary>
        public string ordsn { get; set; }
        /// <summary>
        /// 订单时间
        /// </summary>
        public string ordtime { get; set; }
        /// <summary>
        /// 头像
        /// </summary>
        public string uheard { get; set; }
        /// <summary>
        /// 餐桌号
        /// </summary>
        public string disksn { get; set; }
        public string ordid { get; set; }
        /// <summary>
        /// 1线上订单2线下订单
        /// </summary>
        public string is_online { get; set; }
        /// <summary>
        /// 订单状态
        /// </summary>
        public string codestatus { get; set; }
        /// <summary>
        /// 1管理员2收银3工作员
        /// </summary>
        public string stype { get; set; }

        private void ordercontrol_Load(object sender, EventArgs e)
        {
            //if (!string.IsNullOrEmpty(lbldisksn.Text))
            //{
            //    lbldisksn.Text = "桌号：" + lbldisksn.Text;
            //}
            //else
            //{
            //    if (lblis_online.Text == "1")
            //        lbldisksn.Text = "线上订单";
            //    else
            //        lbldisksn.Text = "线下订单";
            //}

            //lblordsn.Text = "订单号：" + lbl_sn.Text;
            //if (string.IsNullOrEmpty(lbltel.Text))
            //lbltel.Text = "现场下单";
            if (lblreder.Text == "0")
                btncodes.Visible = true;
            if (lblu_sage.Text == "1")
                lblu_sage.Text = "到店";
            else if (lblu_sage.Text == "2")
                lblu_sage.Text = "物流";
            else if (lblu_sage.Text == "3")
                lblu_sage.Text = "外卖";
            //if (codestatus == "2")
            //    btncodes.Visible = true;

            //lbldisksn.Text = "餐桌号：" + disksn;//餐桌号
            lblordsn.Text = lbl_sn.Text;//订单号
            //lbltime.Text = ordtime;//订单时间
            //lbltel.Text = nickname;//昵称
        }
        /// <summary>
        /// 查看订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btninfo_Press(object sender, EventArgs e)
        {
            OrdinfoForm frm = new OrdinfoForm();
            frm.ordsn = lbl_sn.Text;//订单号
            this.Form.Show(frm, true, (obj, args) =>
            {
                //if (stype == "1")
                ((MainForm)this.Form).Getdata();//刷新数据

            });
        }
        /// <summary>
        /// 核销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btncodes_Press(object sender, EventArgs e)
        {
            //OrdinfoForm frm = new OrdinfoForm();
            //frm.ordid = ordid;//订单编号
            //this.Form.Show(frm, true);
            if (orderDal.Updatereder(lbl_sn.Text) > 0)
            {
                ((MainForm)this.Form).StopAudio();//停止播放
                btncodes.Visible = false;
                //((MainForm)this.Form).Getdata();//刷新数据
            }
        }
        /// <summary>
        /// 小票打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnprint_Press(object sender, EventArgs e)
        {
            ms_order order = orderDal.Querybysn(lbl_sn.Text);
            ms_merchant merchant = merchantDal.Querybyid(order.merchant_id);
            int count = 0;
            string printtxt = merchant.merchant_name + "\r\n";
            string str2 = "收货人：" + order.consignee_name;
            string str6 = "电话：" + order.consignee_tel.Substring(0, 3) + "*****" + order.consignee_tel.Substring(8, 3);
            string str3 = "------------------------";
            string str4 = "时间：" + order.add_time;
            string str5 = "编号：" + order.order_sn.Substring(8);
            string str7 = "------------------------";
            string str10 = "------------------------";
            string str11 = "优惠：-￥" + order.coupon_amount;
            string str12 = "配送：￥" + order.logistics_amount;
            string str13 = "打包：￥" + order.packing_amount;
            string str14 = "实付：￥" + order.order_price;
            string str15 = "星鲜厨 正儿八经吃个饭";
            string str16 = "备注：" + order.orderremark;

            List<string> strlist = new List<string>();

            strlist.Add(str2);
            strlist.Add(str3);
            strlist.Add(str4);
            strlist.Add(str5);
            strlist.Add(str6);
            strlist.Add(str7);
            JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
            List<Gdpro> prolist = js.Deserialize<List<Gdpro>>(order.goods_info);

            for (int i = 0; i < prolist.Count; i++)
            {
                //string proname = "";

                //int totalLength = 8 - prolist[i].proname.Length; // 指定字符串加空白后的总长度
                //switch (totalLength)
                //{
                //    case 1:
                //        proname = prolist[i].proname + "  ";
                //        break;
                //    case 2:
                //        proname = prolist[i].proname + "    ";
                //        break;
                //    case 3:
                //        proname = prolist[i].proname + "       ";
                //        break;
                //    case 4:
                //        proname = prolist[i].proname + "          ";
                //        break;
                //    case 5:
                //        proname = prolist[i].proname + "           ";
                //        break;
                //}
                //string pstr = proname + "x" + prolist[i].count + " ￥" + prolist[i].price;
                //strlist.Add(pstr);

                string proname = "";
                char paddingChar = ' '; // 指定空白字符为空格
                int totalLength = 8 - prolist[i].proname.Length; // 指定字符串加空白后的总长度
                if (prolist[i].proname.Length < 8)
                    proname = prolist[i].proname.PadRight(totalLength, paddingChar);
                string pstr = proname + "  x" + prolist[i].count + " ￥" + prolist[i].price;
                strlist.Add(pstr);
            }

            strlist.Add(str10);
            strlist.Add(str11);
            strlist.Add(str12);
            strlist.Add(str13);
            strlist.Add(str14);
            strlist.Add(str15);
            if (!string.IsNullOrEmpty(order.orderremark))
                strlist.Add(str16);
            for (int i = 0; i < strlist.Count; i++)
            {
                //count = (i + 1) * 35;
                printtxt += strlist[i] + "\r\n";
            }
            //int height = Convert.ToInt32(strlist.Count * 4.8);
            //P390PrinterEntityCollection Commands1 = new P390PrinterEntityCollection();
            //Commands1.Add(new P390PrinterRawProtocolEntity("SIZE 57 mm," + height + " mm\r\n" + "REFERENCE 0,0\r\n" + "SPEED 4.0\r\n" + "DENSITY 8\r\n" + "SET PEEL OFF\r\n" + "SET CUTTER OFF\r\n" + "SET TEAR ON\r\n" + "DIRECTION 0\r\n" + "SHIFT 0\r\n" + "OFFSET 0 mm\r\n" + "CLS\r\n" + printtxt + "PRINT 1,1\r\n"));


            //p390Printer1.Print(Commands1, (obj, args) =>
            //{
            //    if (args.isError == true)
            //        this.Toast("Error: " + args.error);
            //    else
            //        this.Toast("打印成功");
            //});
            Print(printtxt);
        }
        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="printxt"></param>
        private void Print(Object printxt)
        {
            //string ptstr = printxt.ToString();
            //P390PrinterEntityCollection Commands1 = new P390PrinterEntityCollection();
            //Commands1.Add(new P390PrinterRawProtocolEntity("SIZE 57 mm, 15 mm\r\n" + "REFERENCE 0,0\r\n" + "SPEED 4.0\r\n" + "DENSITY 8\r\n" + "SET PEEL OFF\r\n" + "SET CUTTER OFF\r\n" + "SET TEAR ON\r\n" + "DIRECTION 0\r\n" + "SHIFT 0\r\n" + "OFFSET 0 mm\r\n" + "CLS\r\n" + ptstr + "PRINT 1,1\r\n"));

            //p390Printer1.Print(Commands1, (obj, args) =>
            //{
            //    if (args.isError == true)
            //        this.Toast("Error: " + args.error);
            //    else
            //    {

            //    }
            //});
            P800PrintEntityCollection entityblueslection = new P800PrintEntityCollection();

            //文本打印
            P800PrintTextEntity text = new P800PrintTextEntity()
            {
                Content = printxt.ToString(),
                Offset = P800PrintOffSet.Left,
                IsBold = false,
                CharacterMultipleX = 1,
                CharacterMultipleY = 0,
                IsUnderLine = false,

            };
            entityblueslection.Add(text);
            //第一个参数：打印指令集，第二个参数：是否切纸，第三个参数：回调
            p800Printer1.Print(entityblueslection, true, (obj, args) =>
            {
                if (args.isError == true)
                    this.MessageBox.Show("Error: " + args.error);
                else
                    this.MessageBox.Show("success");

            });
        }
    }
}